#include "QNBUserInfo.h"

#include <QNetworkInterface>



QNBUserInfo *QNBUserInfo::gUserInfo = NULL;

QNBUserInfo* QNBUserInfo::getInstance()
{
    if (gUserInfo == NULL) {
        gUserInfo = new QNBUserInfo();
    }

    return gUserInfo;
}

QNBUserInfo::QNBUserInfo()
{
    clear();
    test();
}

QString QNBUserInfo::getCpuId()
{
    QString cpu_id = "0000000";
//    unsigned int dwBuf[4];
//    unsigned long long ret;
//    getcpuid(dwBuf, 1);
//    ret = dwBuf[3];
//    ret = ret << 32;
//    cpu_id = QString::number(dwBuf[3], 16).toUpper();
//    cpu_id = cpu_id + QString::number(dwBuf[0], 16).toUpper();

    return cpu_id;
}

void QNBUserInfo::test()
{
    QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();
    QString strMac = "";
    QString strIP = "";
    bool bFound = false;
    foreach(QNetworkInterface item, list) {

        if (item.flags().testFlag(QNetworkInterface::IsUp)
                && !item.flags().testFlag(QNetworkInterface::IsLoopBack))
        {
            if (!(item.humanReadableName().contains("VMware",Qt::CaseInsensitive)))
            {
                strMac = item.hardwareAddress();
                QList<QNetworkAddressEntry> entryList = item.addressEntries();
                foreach(QNetworkAddressEntry entry,entryList) {
                    strIP = entry.ip().toString().trimmed();
                    QString strTmp = strIP.left(3);
                    if(!strIP.isEmpty()) {
                        if (strTmp == "10." || strTmp == "172" || strTmp == "192") {
                            bFound = true;
                        }
                    }

                }
            }

        }

        if (bFound) {
            break;
        }
    }

    if(!bFound) {
        strIP = strMac;
    }

    if (strIP.isEmpty()) {
        strIP = getCpuId();
    }


    mstrClientId = strIP;
    qDebug()<<"IP: "<<strIP;


//    mstrClientId = "001";
//    mstrIdentify = "350212198505111111";
}

void QNBUserInfo::clear()
{
    mLoginStatus = ENUM_LOGIN_BEGIN;
    mForceAction = ENUM_FORCE_UNKNOW;
    mstrIdentify = "";
    //mstrClientId = "";
    mstrAuthPhoto = "";
    mstrLocalAuthPhoto = "";
    mstrRealPhoto = "";

    this->miHeartTime = this->miLoginTime = 0;

    miRealCheckPhotoSimilar = 0;
    miCheckPhotoErrorCount = 0;

}


bool QNBUserInfo::deleteUserData()
{
    QString strPath = QNBConfig::getInstance()->getUserDataDir();

    return QNBUtls::deleteDir(strPath);
}
